﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LaneBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILaneDA LaneDA = DataAccess.CreateLaneDA();
        private PubParasInfo pubParasInfo;

        public LaneBLL()
        {
            this.pubParasInfo = null;
        }

        public LaneBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LaneBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LaneInfo> laneInfos = new List<LaneInfo>();
                LaneInfo laneInfo = null;
                foreach (string id in IDS)
                {
                    laneInfo = LaneDA.GetLaneByID(dataBase, tran, id, this.dataBaseID[0]);
                    laneInfos.Add(laneInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (LaneDA.DeleteLane(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LaneInfo>(dataBase, tran, laneInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LANE", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return LaneDA.GetAllLane(this.dataBaseID[0]);
        }

        public LaneInfo GetInfoByID(string id)
        {
            return LaneDA.GetLaneByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(LaneQueryEntity laneQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(laneQuery.LaneID))
            {
                sqlWhere.Add(" LANE_ID " + laneQuery.LaneID);
            }
            if (!string.IsNullOrEmpty(laneQuery.LaneType))
            {
                sqlWhere.Add(" LANE_TYPE " + laneQuery.LaneType);
            }
            if (!string.IsNullOrEmpty(laneQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + laneQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(laneQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + laneQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(laneQuery.Door))
            {
                sqlWhere.Add(" DOOR " + laneQuery.Door);
            }
            if (!string.IsNullOrEmpty(laneQuery.Status))
            {
                sqlWhere.Add(" STATUS " + laneQuery.Status);
            }
            if (!string.IsNullOrEmpty(laneQuery.LaneSeq))
            {
                sqlWhere.Add(" LANE_SEQ " + laneQuery.LaneSeq);
            }
            if (!string.IsNullOrEmpty(laneQuery.OhType))
            {
                sqlWhere.Add(" OH_TYPE " + laneQuery.OhType);
            }
            return LaneDA.GetLaneByQueryList(sqlWhere, laneQuery, this.dataBaseID[0]);
        }

        public string Insert(LaneInfo lane)
        {
            string LaneID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!string.IsNullOrEmpty(lane.LaneID))
                {
                    if (LaneDA.CheckLaneIDUnique(dataBase, tran, lane.LaneID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F79610_001", this.pubParasInfo.UserLanguage, new List<string> { lane.LaneID });
                    }
                }
                else
                {
                    lane.LaneID = commonMethod.GenNextNumStr(dataBase, tran, "LANE", this.dataBaseID[0]);
                }
                lane.UpdatedBy = lane.CreatedBy;
                lane.CreatedDate = DateTime.Now;
                lane.UpdatedDate = DateTime.Now;
                if (LaneDA.InsertLane(dataBase, tran, lane, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LaneInfo>(dataBase, tran, null, lane, AuditAction.Insert, this.pubParasInfo.UserID, "LANE", this.dataBaseID[0]);
                    LaneID = lane.LaneID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LaneID;
        }

        public string Update(LaneInfo lane)
        {
            string LaneID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                lane.UpdatedDate = DateTime.Now;
                LaneInfo oldLaneInfo = LaneDA.GetLaneByID(dataBase, tran, lane.LaneID, this.dataBaseID[0]);
                if (LaneDA.UpdateLane(dataBase, tran, lane, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LaneInfo>(dataBase, tran, oldLaneInfo, lane, AuditAction.Update, this.pubParasInfo.UserID, "LANE", this.dataBaseID);
                    LaneID = lane.LaneID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return LaneID;
        }
    }
}

